﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LWrap.IndexSource;
namespace LWrap.Index
{
    internal class IndexData
    {
        internal int CreateIndex(IndexDataSource dataSource,IndexWriter indexWriter)
        {
            return this.IndexField(dataSource,indexWriter);
        }

        private int IndexField(IndexDataSource source,IndexWriter indxWriter)
        {
            //List<Lucene.Net.Documents.Document> docs = new List<Lucene.Net.Documents.Document>();
            //Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.GetDirectory(, _isCreateNew);

            while (source.GetNextDoc())
            {
                Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
                doc = this.PrepareField(source.DocFields, doc);
                indxWriter.Execute(doc);
            }
            indxWriter.Dispose();
            return 1;
        }

        private Lucene.Net.Documents.Document PrepareField(string docFields,Lucene.Net.Documents.Document document)
        {
            string[] delim = new string[1];
            delim[0] = string.Empty;
            delim[0] = "\t";
            string[] fields = docFields.Split(delim, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i <fields.Length-1;i=i+2 )
            {
                Lucene.Net.Documents.Field fldContent = new Lucene.Net.Documents.Field(fields[i], fields[i+1], Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED, Lucene.Net.Documents.Field.TermVector.YES);
                document.Add(fldContent);
            }

            return document;

        }
    }
}
